<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"/>
    <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
    <link rel="shortcut icon" href="/favicon.ico">
    <!-- 标题 -->
    <title>论坛系统 - 用户登录</title>
    <!-- 导入CSS -->
    <link href="./dist/css/tabler.min.css?1674944402" rel="stylesheet"/>
    <link rel="stylesheet" href="./dist/css/jquery.toast.css">
    <style>
        #inputCaptcha {
          height: 38px;
          vertical-align: middle;
        }
        #verificationCodeImg{
          vertical-align: middle;
          margin-left: 10px;
        }
    </style>
</head>
<body class="d-flex flex-column">
<!-- 正文 -->
<div class="page page-center">
    <div class="container container-normal py-4">
        <div class="row align-items-center g-4">
            <div class="col-lg">
                <div class="container-tight">
                    <div class="text-center mb-4">
                        <img src="./image/my-logo.png" height="50" alt="">
                    </div>
                    <div class="card card-md">
                        <div class="card-body">
                            <h2 class="text-center mb-4">用户登录</h2>
                            <form id="signInForm" method="get" autocomplete="off" novalidate>
                                <div class="mb-3">
                                    <label class="form-label required">用户名</label>
                                    <input type="text" class="form-control" placeholder="请输入用户名" autocomplete="off" name="username" id="username">
                                    <div class="invalid-feedback">用户名不能为空</div>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label required">
                                        密码
                                    </label>
                                    <div class="input-group input-group-flat">
                                        <input type="password" class="form-control"  placeholder="请输入密码"  autocomplete="off" name="password"
                                               id="password">
                                        <span class="input-group-text">
                            <a href="javascript:void(0);" id="password_a" class="link-secondary" title="显示密码" data-bs-toggle="tooltip">
                              <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M12 12m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0" /><path d="M22 12c-2.667 4.667 -6 7 -10 7s-7.333 -2.333 -10 -7c2.667 -4.667 6 -7 10 -7s7.333 2.333 10 7" /></svg>
                            </a>
                          </span>
                                        <div class="invalid-feedback">密码不能为空</div>
                                    </div>
                                </div>
                                <!-- 验证码区域 -->
                                <div class="mb-3">
                                    <label class="form-label required">验证码</label>
                                    <div class="input-group">
                                        <input type="text" class="form-control" id="inputCaptcha" placeholder="请输入验证码" autocomplete="off">
                                        <img id="verificationCodeImg" src="/captcha/get" style="cursor: pointer; height: 38px;" title="看不清？换一张" />
                                        <div class="invalid-feedback">验证码不能为空</div>
                                    </div>
                                </div>
                                <div class="form-footer">
                                    <button id="submit" type="button" class="btn btn-primary w-100">登录</button>
                                </div>
                            </form>
                        </div>
                    </div>
                    <div class="text-center text-muted mt-3">
                        还没有注册吗? <a href="./sign-up.html" tabindex="-1">点击注册</a>
                    </div>
                </div>
            </div>
            <div class="col-lg d-none d-lg-block">
                <img src="./dist/illustrations/my-illustration.jfif" height="300" class="d-block mx-auto" alt="">
            </div>
        </div>
    </div>
</div>
</body>
<!-- 导入JS -->
<script src="./dist/js/jquery-3.6.3.min.js"></script>
<script src="./dist/js/tabler.min.js"></script>
<script src="./dist/js/jquery.toast.js"></script>
<script>
    $(function () {
      // 获取控件
      let usernameEl = $('#username');
      let passwordEl = $('#password');
      let captchaEl = $('#inputCaptcha');
      let captchaImg = $('#verificationCodeImg');

      // 验证码刷新功能
      captchaImg.click(function(){
        $(this).hide().attr('src', '/captcha/get?dt=' + new Date().getTime()).fadeIn();
      });

      // 登录校验
      $('#submit').click(function () {
        let checkForm = true;

        // 校验用户名
        if (!usernameEl.val()) {
          usernameEl.addClass('is-invalid');
          checkForm = false;
        }

        // 校验密码
        if (!passwordEl.val()) {
          passwordEl.addClass('is-invalid');
          checkForm = false;
        }

        // 校验验证码
        if (!captchaEl.val()) {
          captchaEl.addClass('is-invalid');
          checkForm = false;
        }

        if (!checkForm) {
          return false;
        }

        // 先验证验证码
        $.ajax({
          type: "post",
          url: "/captcha/check",
          data: {
            captchaCode: captchaEl.val()
          },
          success: function(result){
            if(result){
              // 验证码正确，提交登录信息
              submitLoginForm();
            }else{
              $.toast({
                heading: '警告',
                text: '验证码错误',
                icon: 'warning'
              });
              // 刷新验证码
              captchaImg.click();
              captchaEl.val('');
            }
          },
          error: function(){
            $.toast({
              heading: '错误',
              text: '验证码验证失败，请重试',
              icon: 'error'
            });
          }
        });
      });

      // 提交登录表单
      function submitLoginForm() {
        let postData = {
          username : usernameEl.val(),
          password : passwordEl.val()
        };

        $.ajax({
          type: 'post',
          url : 'user/login',
          contentType : 'application/x-www-form-urlencoded',
          data : postData,
          success : function (respData) {
            if (respData.code == 0){
              location.assign('/index.html');
            }else {
              $.toast({
                heading: '警告',
                text: respData.message,
                icon: 'warning'
              });
              // 登录失败时刷新验证码
              captchaImg.click();
              captchaEl.val('');
            }
          },
          error : function (){
            $.toast({
              heading: '错误',
              text: '访问出现问题，请与管理员联系.',
              icon: 'error'
            });
          }
        });
      }

      // 表单元单独检验
      $('#username, #password, #inputCaptcha').on('blur', function () {
        if ($(this).val()) {
          $(this).removeClass('is-invalid');
          $(this).addClass('is-valid');
        } else {
          $(this).removeClass('is-valid');
          $(this).addClass('is-invalid');
        }
      });

      // 显示/隐藏密码
      $('#password_a').click(function () {
        if(passwordEl.attr('type') == 'password') {
          passwordEl.attr('type', 'text');
        } else {
          passwordEl.attr('type', 'password');
        }
      });
    });
</script>
</body>
</html>